<template>
  <div class="order-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button type="primary" @click="handleRefund">
          处理退款
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select v-model="queryType">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="query"
              placeholder="请输入搜索内容"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="orderList"
      :element-loading-text="elementLoadingText"
    >
      <el-table-column
        show-overflow-tooltip
        width="250"
        prop="orderID"
        label="订单号"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="wxName"
        label="微信名"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="contactName"
        label="联系人"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="phone"
        label="联系电话"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="time"
        label="下单时间"
      ></el-table-column>
      <el-table-column 
        show-overflow-tooltip 
        label="订单状态" 
        prop="state"
      ></el-table-column>
      <el-table-column 
        show-overflow-tooltip 
        label="订单金额" 
        prop="money"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row.orderID)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row.orderID, row.index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    
  </div>
</template>

<script>
  import { getOrderList, delOrder} from '@/api/order'
  import Edit from './components/OrderManagementEdit'

  export default {
    name: 'OrderManagement',
    components: { Edit },
    data() {
      return {
        orderList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          orderID: "",
          contactName: "",
          state: "",
          current: 1,
          size: 10
        },
        query: "",
        queryType: 1,
        options: [{
          value: 1,
          label: '订单号'
        }, {
          value: 2,
          label: '联系人姓名'
        }, {
          value: 3,
          label: '订单状态'
        }]
      }
    },
    created() {
      this.getOrder()
    },
    methods: {
      async getOrder() {
        this.listLoading = true
        const {data} = await getOrderList(this.queryForm)
        this.orderList = data.records
        this.total = data.total
        this.listLoading = false
      },
      handleRefund() {
        const url = "https://pay.weixin.qq.com/index.php/core/refundapply"
        window.open(url)
      },
      handleEdit(orderID) {
        this.$refs['edit'].showEdit(orderID)
      },
      handleDelete(orderID,index) {
        this.$confirm('您确定要删除该订单吗?删除的订单将不可恢复', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteOrder(orderID,index)
        });
      },
      async deleteOrder(orderID,index){
        this.listLoading = true
        const {code} = await delOrder(orderID)
        this.listLoading = false
        if (code===1000) {
          this.orderList.splice(index,1)
          this.$message({
            type: 'success',
            message: '删除成功'
          });     
        } else {
          this.$message.error('删除失败，请检查网络是否正常');
        }
      },
      handleSizeChange(val) {
        this.queryForm.size = val
        this.getOrder()
      },
      handleCurrentChange(val) {
        this.queryForm.current = val
        this.getOrder()
      },
      queryData() {
        this.queryForm.current = 1
        if (this.queryType==1) {
          this.queryForm.orderID = this.query
          this.queryForm.contactName = ""
          this.queryForm.state = ""
        } else if (this.queryType==2) {
          this.queryForm.contactName = this.query
          this.queryForm.orderID = ""
          this.queryForm.state = ""
        } else if (this.queryType==3) {
          this.queryForm.state = this.query
          this.queryForm.contactName = ""
          this.queryForm.orderID = ""
        }
        this.getOrder()
      },
    },
  }
</script>
